/**
 * Copyright(c) 2013 Jade Techonologies Co., Ltd.
 *
 * History:
 *   13-4-14 下午3:44 Created by dwzhang
 */
package com.jade.framework.base.criterion;

import java.io.Serializable;

/**
 * Represents an order imposed upon a Criteria result set.
 *
 * @author dwzhang
 * @version 1.0 13-4-14 下午3:44
 */
public class Order implements Serializable
{
    protected final String entityName;
    protected final String propName;
    protected final boolean ascending;

    private Order(String entityName, String propName, boolean ascending)
    {
        this.entityName = entityName;
        this.propName = propName;
        this.ascending = ascending;
    }

    public String getPropName()
    {
        return propName;
    }

    public String getEntityName()
    {
        return propName;
    }

    public String getFullName()
    {
        if (entityName == null) {
            return propName;
        }
        else {
            return entityName + "." + propName;
        }
    }

    public boolean isAscending()
    {
        return ascending;
    }

    public static Order asc(String name)
    {
        return new Order(null, name, true);
    }

    public static Order asc(String entityName, String name)
    {
        return new Order(entityName, name, true);
    }

    public static Order desc(String name)
    {
        return new Order(null, name, false);
    }

    public static Order desc(String entityName, String name)
    {
        return new Order(entityName, name, false);
    }

    @Override
    public String toString()
    {
        StringBuilder sb = new StringBuilder();
        sb.append(getFullName());
        sb.append(" ");
        if (isAscending()) {
            sb.append(Constants.ASC);
        }
        else {
            sb.append(Constants.DESC);
        }
        return sb.toString();
    }
}
